GBML manual / Grids

grid_fill_region

Sets a block of cells of target to value: every cell from column x1, row y1 across and down to column x2, row y2, both corners included, so (1, 1) to (3, 2) is 3 cells wide and 2 tall. A block with its corners the wrong way round (x2 less than x1, or y2 less than y1) is empty and changes nothing. A block that runs off the grid is cut to the grid. Typing a corner that is off the grid, or the corners the wrong way round, is refused when the project is checked.

Syntax

grid_fill_region(target, x1, y1, x2, y2, value)

Arguments

NameType
targetgrid
x1int
y1int
x2int
y2int
valueelement

Returns

Nothing.

Description

Sets a block of cells of target, a grid, to value. The block is a rectangle. (x1, y1) is its top-left cell and (x2, y2) is its bottom-right cell, and both of those cells are included.

grid_fill_region(board, 1, 1, 3, 2, 9);   // columns 1 to 3, rows 1 to 2

That block is 3 cells across and 2 down, so it fills 6 cells. A block of one cell is the same corner twice: grid_fill_region(board, 2, 3, 2, 3, 9) fills just the cell at column 2, row 3. To fill every cell, use grid_fill.

x counts columns across, starting at 0 on the left. y counts rows down, starting at 0 at the top. It is the same order as board[# x, y].

The rules for the corners

These rules are the same for grid_get_region_sum, grid_get_region_max and grid_get_region_mean, which use the same kind of block.

Both corners are included. (1, 1) to (3, 2) is columns 1, 2 and 3 and rows 1 and 2.

Corners the wrong way round give an empty block. If x2 is less than x1, or y2 is less than y1, there are no cells between them, so nothing is filled and nothing changes. The corners are not swapped for you: a block that turned itself round could hide a mistake. A block with x2 one less than x1 is empty too, which is just what you want when a loop shrinks a block down to nothing.

A block that runs off the grid is cut to the grid. The cells that are on the grid are filled, and the rest are ignored. It never wraps round onto the next row, never reaches back into the row above, and never writes outside the grid. A block that is wholly off the grid is empty.

If the numbers are typed in the code, a mistake is an error. When you type a number straight into the call, GBML can check it before the game runs. A corner that is off the grid, like 8 on a grid with 8 columns (the last column is 7), or two corners the wrong way round, is refused with a message. Numbers that are worked out while the game runs, like a variable named column, or grid_width(board) - 1, cannot be checked ahead of time, so for those the cut-to-the-grid and empty-block rules above apply instead.

The blockWhat happens
(1, 1) to (3, 2) on an 8 by 6 grid6 cells filled
(2, 2) to (2, 2)the one cell, (2, 2), filled
x2 or y2 before x1 or y1, worked out while the game runsnothing filled
x2 or y2 past the edge, worked out while the game runsfilled up to the edge only
x1 or y1 below 0, worked out while the game runsfilled from the edge only
a corner typed as a number that is off the gridrefused when checked
corners typed the wrong way roundrefused when checked

To fill a block that reaches the right edge or the bottom edge without counting, use grid_width and grid_height: the last column is grid_width(board) - 1 and the last row is grid_height(board) - 1.

The grid has to know its size

To find a block, GBML needs to know how many cells are in a row. A grid only knows its width and height where it was made (grid_create(8, 6, 0), /// @capacity 8x6, or grid<int, 8, 6>). A grid with no size written like that, or a script's grid parameter, has none to give, and the call is refused, the same as grid_width is. In a script, grid_fill still works on the whole grid.

What happens in the awkward cases

You do thisWhat happens
give a value of the wrong kindrefused when checked, and the message says what kind the grid holds
give a corner with a decimal point, like 1.5refused when checked. A corner is a whole number
give it a list or an arrayrefused when checked
give it the wrong number of argumentsrefused when checked
use it where a value is neededrefused when checked. It gives nothing back

Each of the five numbers and the value is worked out exactly once, in the order written. The value is copied before any cell changes.

What it costs

It visits every cell in the block once, so the time grows with the number of cells in the block, not the size of the grid.

Example

var board = grid_create(8, 6, 0);
grid_fill_region(board, 1, 1, 3, 2, 9);                         // 3 across and 2 down: 6 cells
grid_fill_region(board, 0, 0, grid_width(board) - 1, 0, 1);     // the whole top row

On the GBA

Two loops over the block, assigning a copy of value to each cell - O(cells in the block). It needs the grid's width, which a flat vector does not carry, so it takes the width and height the checker reads from the grid's declaration (`grid_create(8, 6, 0)`, `/// @capacity 8x6` or `grid<int, 8, 6>`) and writes them into the C++ as plain numbers - the same two numbers [# x, y] uses. A grid with none (a script's parameter, one declared with no shape) is refused. The four numbers are worked out once each, in the order written, then held to 0..width-1 and 0..height-1, so the loops cannot leave the grid and a block that runs off one edge never wraps onto the next row; the flat index is also checked against the vector's real size. Numbers typed straight into the call that are off the grid, or corners typed the wrong way round, are refused when the project is checked. value is worked out once and copied before the first cell changes.

See also: grid_fillgrid_get_region_sumgrid_get_region_maxgrid_get_region_meangrid_widthgrid_heightgrid_createGridsData containers